﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using QLST_DTO;

namespace QLST_DAO
{
    public class NHANVIENDAO : TempDAO
    {
        public NHAN_VIEN DangNhap(NHAN_VIEN nv)
        {
            NHAN_VIEN NV = new NHAN_VIEN();
            SqlParameter[] parameters = new SqlParameter[2];
            parameters[0] = new SqlParameter("@TenDangNhap", nv.TenDangNhap);
            parameters[1] = new SqlParameter("@MatKhau", nv.MatKhau);
            DataTable tbl = dap.ExcuteProcedureGetTable("st_DangNhap", parameters);
            if (tbl.Rows.Count > 0)
            {
                DataRow dr = tbl.Rows[0];
                NV.HoTen = dr["HoTen"].ToString();
                NV.MaLoaiNV = int.Parse(dr["MaLoaiNV"].ToString());
                NV.MaNV = int.Parse(dr["MaNV"].ToString());
                NV.MatKhau = dr["MatKhau"].ToString();
                NV.NgayVaoLamViec = System.DateTime.Parse(dr["NgayVaoLamViec"].ToString());
                NV.SoDienThoai = dr["SoDienThoai"].ToString();
                NV.TenDangNhap = dr["TenDangNhap"].ToString();
                NV.CMND = int.Parse(dr["CMND"].ToString());
                NV.DiaChiLienLac = dr["DiaChiLienLac"].ToString();

            }
            return NV;
        }

        /// <summary>
        /// Lay Danh Sach Nhan Vien
        /// </summary>
        /// <returns></returns>
        public List<NHAN_VIEN> GetDs()
        {
            List<NHAN_VIEN> lst = new List<NHAN_VIEN>();
            DataTable tbl = dap.ExcuteProcedureGetTable("st_SelectDsNhanVien", null);
            foreach (DataRow dr in tbl.Rows)
            {
                NHAN_VIEN NV = new NHAN_VIEN();
                NV.HoTen = dr["HoTen"].ToString();
                NV.MaLoaiNV = int.Parse(dr["MaLoaiNV"].ToString());
                NV.MaNV = int.Parse(dr["MaNV"].ToString());
                NV.MatKhau = dr["MatKhau"].ToString();
                NV.NgayVaoLamViec = System.DateTime.Parse(dr["NgayVaoLamViec"].ToString());
                NV.SoDienThoai = dr["SoDienThoai"].ToString();
                NV.TenDangNhap = dr["TenDangNhap"].ToString();
                NV.CMND = int.Parse(dr["CMND"].ToString());
                NV.DiaChiLienLac = dr["DiaChiLienLac"].ToString();
                lst.Add(NV);
            }
            return lst;
        }

        /// <summary>
        /// Them nhan vien
        /// </summary>
        /// <param name="nv"></param>
        /// <returns></returns>
        public int Insert(NHAN_VIEN nv)
        {
            SqlParameter[] para = new SqlParameter[]{
                                                    new SqlParameter("@HoTen",nv.HoTen),
                                                    new SqlParameter("@CMND",nv.CMND),
                                                    new SqlParameter("@NgayVaoLamViec",nv.NgayVaoLamViec),
                                                    new SqlParameter("@TenDangNhap",nv.TenDangNhap),
                                                    new SqlParameter("@MatKhau",nv.MatKhau),
                                                    new SqlParameter("@SoDienThoai",nv.SoDienThoai),
                                                    new SqlParameter("@DiaChiLienLac",nv.DiaChiLienLac),
                                                    new SqlParameter("@MaLoaiNV",nv.MaLoaiNV)};
            return dap.ExcuteProcedureWithReturn("st_InsertNhanVien", para);
        }

        /// <summary>
        /// Cap nhat thong tin nhan vien
        /// </summary>
        /// <param name="nv"></param>
        /// <returns></returns>
        public int Update(NHAN_VIEN nv)
        {
            SqlParameter[] para = new SqlParameter[]{
                                                    new SqlParameter("@MaNV",nv.MaNV),
                                                    new SqlParameter("@HoTen",nv.HoTen),
                                                    new SqlParameter("@CMND",nv.CMND),
                                                    new SqlParameter("@NgayVaoLamViec",nv.NgayVaoLamViec),
                                                    new SqlParameter("@TenDangNhap",nv.TenDangNhap),
                                                    new SqlParameter("@MatKhau",nv.MatKhau),
                                                    new SqlParameter("@SoDienThoai",nv.SoDienThoai),
                                                    new SqlParameter("@DiaChiLienLac",nv.DiaChiLienLac),
                                                    new SqlParameter("@MaLoaiNV",nv.MaLoaiNV)};
            return dap.ExcuteProcedureWithReturn("st_UpdateNhanVien", para);
        }

        /// <summary>
        ///Xoa Nhan vien 
        /// </summary>
        /// <param name="MaNV"></param>
        /// <returns></returns>
        public int Delete(int MaNV)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@MaNV", MaNV) };
            return dap.ExcuteProcedureWithReturn("st_DeleteNhanVien", para);
        }


    }
}
